﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace SqlSugarMod;
/// <summary>
/// 数据库访问基类
/// </summary>
public class PGSql
{
    public static SqlSugarScope PG_db = null;
    /// <summary>
    /// 初始化数据库连接
    /// </summary>
    /// <param name="server">主机IP</param>
    /// <param name="port">端口</param>
    /// <param name="database">数据库名称</param>
    /// <param name="userId">用户名</param>
    /// <param name="passWord">密码</param>
    /// <param name="ifJiaMi">是否加密</param>
    public static void InitialPGSql(string server, string port, string database, string userId, string passWord, bool ifJiaMi)
    {

        //if (ifJiaMi)
        //{
        //    passWord = Encryption.DecryptStringFromBytes_Aes(passWord, "PostgreSQLExample");//解密密码
        //}
        PG_db = new SqlSugarScope(new ConnectionConfig
        {
            //ConnectionString = $"Server={server};Port={port};Database={database};User Id={userId};Password={passWord}",
            ConnectionString = $"Host={server};Port={port};Database={database};User Id={userId};Password={passWord}",
            DbType = SqlSugar.DbType.PostgreSQL,
            IsAutoCloseConnection = true,
            MoreSettings = new ConnMoreSettings()
            {
                PgSqlIsAutoToLower = false,//增删查改支持驼峰表
                PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30 
            }
        });  // 使用SqlSugarScope建立PostgreSQL数据库连接


    }
    /// <summary>
    /// 获取数据库Postgre当前时间
    /// </summary>
    /// <returns></returns>
    public static DateTime GetPostgreDateTime()
    {
        return PG_db.Ado.GetDateTime("select now();");
    }


}
